<?php

namespace api\ggz\controller;

use api\ggz\model\ConfigModel;
use api\ggz\traits\WechatTokentraite;
use think\facade\Db;
use app\dem\service\SpSignService;
use cmf\controller\RestBaseController;
use think\Request;
use api\ggz\model\CompanyModel;
use api\ggz\model\CustomerModel;
use api\ggz\service\WeChat;
use api\ggz\service\Token;
use api\ggz\model\QualificationsModel;
use api\ggz\model\PatentModel;
use api\ggz\model\MarkModel;


class ApiController extends RestBaseController
{
    protected  $wechat;
    protected $token;
    protected  $uid;

    function __construct()
    {
        parent::__construct();
        $this->wechat = new WeChat();
        $this->token    =   new Token();
        $login  =   $this->token->checkToken($this->request->header("token"));
        /*if($login['code']   ==  500){
            return $this->error($login['msg']);
        }
        $this->uid = $login['uid'];*/
        $this->uid  =   1;
    }

    /**获取选项
     * @param Request $request
     * @param ConfigModel $configModel
     */
    public function getSelect(Request $request,ConfigModel $configModel){
        if($request->param('note')){
            $data   =   $configModel->getConfigByNote($request->param('note'));
        }
      /*  if($request->param('type')){
            $data   =   $configModel->getConfigByType($request->param('type'));
        }*/
        $this->success('获取信息成功！',$data);
    }

    /**更新小程序用户信息
     * @param Request $request
     * @param CustomerModel $customerModel
     */
    public function  setUserInfo(Request $request,CustomerModel $customerModel){
        $data = $request->param();
        if($re  =   $customerModel->save($data,['id'=>$this->uid])){
            return $this->success('请求成功!');
        }
        return  $this->error('无法更新用户信息!',$re);
    }

    /**新增公司
     * @param CompanyModel $companyModel
     * @param Request $request
     */
    public function addCompany(CompanyModel $companyModel,Request $request){
        $data   =   $request->param();
        $data['uid']    =   $this->uid;
        $data['create_time']    =   date("Y-m-d H:i:s");
        $id = $companyModel->insertGetId($data);
        return  $id ? $this->success('请求成功!',['id'=>$id]) : $this->error('请求失败');
    }

    /**更新公司
     * @param CompanyModel $companyModel
     * @param Request $request
     */
    public function updateCompany(CompanyModel $companyModel,Request $request){
        return $companyModel->save($request->param(),['id'=>$request->param('id')]) ? $this->success('请求成功!') : $this->error('请求失败');
    }

    /**新增资质交易信息
     * @param QualificationsModel $QualificationsModel
     * @param Request $request
     */
    public function addQualifications(QualificationsModel $QualificationsModel,Request $request){
        $data   =   $request->param();
        $data['uid']    =   $this->uid;
        $data['create_time']    =   date("Y-m-d H:i:s");
        $id = $QualificationsModel->insertGetId($data);
        return  $id ? $this->success('请求成功!',['id'=>$id]) : $this->error('请求失败');
    }

    /**更新资质交易信息
     * @param QualificationsModel $QualificationsModel
     * @param Request $request
     */
    public function updateQualifications(QualificationsModel $QualificationsModel,Request $request){
        return $QualificationsModel->save($request->param(),['id'=>$request->param('id')]) ? $this->success('请求成功!') : $this->error('请求失败');
    }

    /**新增商标交易信息
     * @param MarkModel $MarkModel
     * @param Request $request
     */
    public function addMark(MarkModel $MarkModel,Request $request){
        $data   =   $request->param();
        $data['uid']    =   $this->uid;
        $data['create_time']    =   date("Y-m-d H:i:s");
        $id = $MarkModel->insertGetId($data);
        return  $id ? $this->success('请求成功!',['id'=>$id]) : $this->error('请求失败');
    }

    /**更新商标交易
     * @param MarkModel $MarkModel
     * @param Request $request
     */

    public function updateMark(MarkModel $MarkModel,Request $request){
        return $MarkModel->save($request->param(),['id'=>$request->param('id')]) ? $this->success('请求成功!') : $this->error('请求失败');
    }

    /**新增专利交易信息
     * @param PatentModel $PatentModel
     * @param Request $request
     */
    public function addPatent(PatentModel $PatentModel,Request $request){
        $data   =   $request->param();
        $data['uid']    =   $this->uid;
        $data['create_time']    =   date("Y-m-d H:i:s");
        $id = $PatentModel->insertGetId($data);
        return  $id ? $this->success('请求成功!',['id'=>$id]) : $this->error('请求失败');
    }

    /**更新专利交易
     * @param PatentModel $PatentModel
     * @param Request $request
     */

    public function updatePatent(PatentModel $PatentModel,Request $request){
        return $PatentModel->save($request->param(),['id'=>$request->param('id')]) ? $this->success('请求成功!') : $this->error('请求失败');
    }

    /**我的公司信息列表
     * @param CompanyModel $companyModel
     */

    public function myConpany(CompanyModel $companyModel){
        $page   =   $this->request->param('page') ?? 1;
        $data   =   $companyModel->where('uid',$this->uid)->order('id desc')->limit(($page-1)*5,5)->select();
        return  $this->success('请求成功!',$data);
    }

    /**我的资质信息列表
     * @param QualificationsModel $qualificationsModel
     */
    public function myQualifications(QualificationsModel $qualificationsModel){
        $page   =   $this->request->param('page') ?? 1;
        $data   =   $qualificationsModel->where('uid',$this->uid)->order('id desc')->limit(($page-1)*5,5)->select();
        return  $this->success('请求成功!',$data);
    }

    /**我的商标
     * @param MarkModel $markModel
     */

    public function myMark(MarkModel $markModel){
        $page   =   $this->request->param('page') ?? 1;
        $data   =   $markModel->where('uid',$this->uid)->order('id desc')->limit(($page-1)*5,5)->select();
        return  $this->success('请求成功!',$data);
    }

    /**我的专利信息列表
     * @param PatentModel $patentModel
     */

    public function myPatent(PatentModel $patentModel){
        $page   =   $this->request->param('page') ?? 1;
        $data   =   $patentModel->where('uid',$this->uid)->order('id desc')->limit(($page-1)*5,5)->select();
        return  $this->success('请求成功!',$data);
    }
}